We will be using the Asia network (http://www.bnlearn.com/bnrepository/#asia) and do some inference queries.
In [1]:
# Fetching the network
import wget
import gzip
f = wget.download('http://www.bnlearn.com/bnrepository/asia/asia.bif.gz')
with gzip.open('asia.bif.gz', mode='rb') as f:
file_content=f.read()
with open('asia.bif', mode='wb') as f:
f.write(file_content)
In [2]:
from pgmpy.readwrite import BIFReader
reader = BIFReader('asia.bif')
!rm asia.bif
asia_model = reader.get_model()
In [3]:
asia_model.nodes()
Out[3]:
In [4]:
asia_model.edges()
Out[4]:
In [5]:
asia_model.get_cpds()
Out[5]:
In [6]:
# Doing exact inference using Variable Elimination
from pgmpy.inference import VariableElimination
asia_infer = VariableElimination(asia_model)
# Computing the probability of bronc given smoke.
q = asia_infer.query(variables=['bronc'], evidence={'smoke': 'no'})
print(q)
In [7]:
q = asia_infer.query(variables=['bronc'], evidence={'smoke': 'yes'})
print(q)
In [ ]: